package com.zlm.linked;

/**
 * @author zlm
 * @date 2025/4/28
 *
 * 链表
 */
public class LinkedNode {

    public static void main(String[] args) {
        Node<Integer> head = new Node<>(1, null);
        //editNode(head);
        addNode(head, 100);
        printNode(head);
        addNode(head, 200);
        printNode(head);
        addNode(head, 300);
        printNode(head);

    }

    private static void printNode(Node<Integer> node) {
        Node<Integer> cur = node;
        while (cur != null) {
            System.out.print(cur.value+"\t");
            cur = cur.next;
        }
        System.out.println();
    }

    private static void editNode(Node<Integer> head) {
        Node<Integer> node2 = new Node<>(2, null);
        //更改指针域
        head.next = node2;
        Node<Integer> node3 = new Node<>(3, null);
        node2.next = node3;
        Node<Integer> node4 = new Node<>(4, null);
        node3.next = node4;
        //指针赋值
        Node<Integer> tmp  = head;
        //指针移动
        tmp = tmp.next;
        //通过tmp指针变量更改节点的指针域：截断链表
        tmp.next = null;
        //通过tmp指针变量更改节点的指针域：增加节点
        tmp.next = new Node<>(100,null);
        head.next.next = null;
    }

    public static <E> void addNode(Node<E> node, E value){
        if(node == null){
            return;
        }
        //创建一个新节点
        Node<E> newNode = new Node<>(value,null);
        Node<E> cur = node;
        while (cur.next != null){
            //移动指针
            cur = cur.next;
        }
        cur.next = newNode;
    }

    static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value, Node<E> next) {
            this.value = value;
            this.next = next;
        }
    }
}
